<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" label-width="220px" :model="userInfo" :rules="rules">
      <!--手机 -->

      <el-form-item label="手机" disabled>
        <el-input v-model="userInfo.mobile" style="width:220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <!-- <img :src="userInfo.staffPhoto"> -->
        <!-- 这里用到了v-model的原理，子组件给父组件传递服务器的传过来的腾讯云路径 -->
        <!-- 父组件要给子组件传递图片的路径，这样可以保证用户更新数据完成后图片依然显示 -->
        <UploadImg v-model="userInfo.staffPhoto" />
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="updateUser">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailInfo, updateUserInfo } from '@/api/user'
export default {
  data() {
    return {
      userId: this.$route.params.id,
      userInfo: {
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      rules: {
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadUserInfo()
  },
  methods: {
    // 获取用户信息
    async loadUserInfo() {
      const res = await getUserDetailInfo(this.$route.params.id).catch(e => e)
      this.userInfo = res.data
    },
    // 修改用户信息
    async updateUser() {
      const valid = await this.$refs.userForm.validate().catch(e => e)
      if (!valid) return
      const res = await updateUserInfo(this.userInfo).catch(e => e)
      this.$message.success(res.message)
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
